package com.ruoyi.system.service.impl;

import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.enums.SaleOrPurchType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.numGene.NumberGene;
import com.ruoyi.system.domain.ColumnItems;
import com.ruoyi.system.domain.ExportParam;
import com.ruoyi.system.domain.SalePlanForWaybill;
import com.ruoyi.system.service.IColumnItemsService;
import com.ruoyi.system.util.handleExcelExport;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SalePlanMapper;
import com.ruoyi.system.domain.SalePlan;
import com.ruoyi.system.service.ISalePlanService;

import javax.servlet.http.HttpServletResponse;

/**
 * 销售计划Service业务层处理
 * 
 * @author he
 * @date 2025-08-14
 */
@Service
public class SalePlanServiceImpl implements ISalePlanService 
{
    @Autowired
    private SalePlanMapper salePlanMapper;
    @Autowired
    private IColumnItemsService columnItemsService;

    /**
     * 查询销售计划
     * 
     * @param id 销售计划主键
     * @return 销售计划
     */
    @Override
    public SalePlan selectSalePlanById(Long id)
    {
        return salePlanMapper.selectSalePlanById(id);
    }

    /**
     * 查询销售计划列表
     * 
     * @param salePlan 销售计划
     * @return 销售计划
     */
    @Override
    public List<SalePlan> selectSalePlanList(SalePlan salePlan)
    {
        return salePlanMapper.selectSalePlanList(salePlan);
    }

    /**
     * 新增销售计划
     * 
     * @param salePlan 销售计划
     * @return 结果
     */
    @Override
    public int insertSalePlan(SalePlan salePlan)
    {
        char[] chars = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
        'q','w','e','r','t','y','u','i','o','p','a','s','d','f','g','h','j','k','l','z','x','c','v','b','n','m'};
        salePlan.setCreateUserId(SecurityUtils.getUserId());
        salePlan.setCreateUserName(SecurityUtils.getUsername());
        salePlan.setCreateTime(DateUtils.getNowDate());

        if (ObjectUtils.isNotEmpty(salePlan.getTotal())){
            salePlan.setRemain(salePlan.getTotal());
        }
        salePlan.setPlanRemaincarQty(salePlan.getPlancarQty());

        if (Long.valueOf(SaleOrPurchType.SALE.getCode()).equals(salePlan.getType())) {
            //生成编号 和取货码
            salePlan.setNumber(NumberGene.generateCode("SSJH"));
            salePlan.setPickCode(RandomStringUtils.random(4,chars));
        } else {
            salePlan.setNumber(NumberGene.generateCode("CGJH"));
            salePlan.setPickCode(RandomStringUtils.random(4,chars));
        }


        return salePlanMapper.insertSalePlan(salePlan);
    }

    /**
     * 修改销售计划
     * 
     * @param salePlan 销售计划
     * @return 结果
     */
    @Override
    public int updateSalePlan(SalePlan salePlan)
    {

        return salePlanMapper.updateSalePlan(salePlan);
    }

    /**
     * 批量删除销售计划
     * 
     * @param ids 需要删除的销售计划主键
     * @return 结果
     */
    @Override
    public int deleteSalePlanByIds(Long[] ids)
    {
        return salePlanMapper.deleteSalePlanByIds(ids);
    }

    /**
     * 删除销售计划信息
     * 
     * @param id 销售计划主键
     * @return 结果
     */
    @Override
    public int deleteSalePlanById(Long id)
    {
        return salePlanMapper.deleteSalePlanById(id);
    }


    /**
     * 根据当前登录人 用户id加角色ID加表名  获取对应的展示数据列及导出数据列
     * @param response
     * @param exportParam
     */
    @Override
    public void exportNew(HttpServletResponse response, ExportParam exportParam) {

        //查找登录人和角色
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUser().getUserId();
        Long roleId = loginUser.getUser().getRoleId();

        exportParam.setRoleId(roleId);
        exportParam.setUserId(userId);
        exportParam.setTableName("销售明细表");

        List<ColumnItems> columnItems = columnItemsService.selectColumnItemsListForExport(exportParam);

        List<ColumnItems> collect = columnItems.stream().sorted(Comparator.comparingLong(ColumnItems::getOrderNo)).collect(Collectors.toList());

        List<SalePlan> salePlans = salePlanMapper.selectSalePlanList(new SalePlan());

        try {
            handleExcelExport.export(response, salePlans, collect, "销售计划");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Override
    public List<SalePlanForWaybill> getPlanNameOptions(SalePlan paramName) {
        return salePlanMapper.getPlanNameOptions(paramName);
    }

    @Override
    public List<SalePlan> selectSalePlanByCode(String code) {
        return salePlanMapper.selectSalePlanByCode(code);
    }

    @Override
    public List<SalePlan> selectSalePlanListByIdList(List<Long> collect) {
        return salePlanMapper.selectSalePlanListByIdList(collect);
    }

    /**
     * 批量更新审批状态
     *
     * @param ids 需要更新的销售/采购计划主键集合
     * @param approvalState 审批状态
     * @return 结果
     */
    @Override
    public int updateApprovalState(List<Long> ids, Integer approvalState) {
        return salePlanMapper.updateApprovalState(ids, approvalState);
    }

}
